/*
 * Copyright 2009 Colin Prinn
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

package net.recursv.motific.at.scene;

import net.recursv.motific.utils.Nullable;

import java.util.Enumeration;
import java.util.Vector;

/**
 * Created by: recursv
 * Date: 9/08/2009
 * Time: 10:24:02 AM
 */
public class AtScene implements Nullable {
    private final Vector _tasks = new Vector();

    public AtScene(Vector tasks) {
        processTasks(tasks);
    }

    public void processTasks(Vector tasks) {
        _tasks.removeAllElements();
        for (int i = 0; i < tasks.size(); i++)
            _tasks.addElement(tasks.elementAt(i));

        Enumeration enumeration = ConstructManager.getConstructs();
        while (enumeration.hasMoreElements()) {
            Construct construct = (Construct) enumeration.nextElement();

            for (int taskIndex = _tasks.size() - 1, reqIndex = construct.size() - 1; taskIndex >= 0; taskIndex--) {
                if (construct.get(reqIndex).matches((GraphicsTask) _tasks.elementAt(taskIndex)))
                    reqIndex--;
                else
                    reqIndex = construct.size() - 1;
                if (reqIndex == -1) {
                    GraphicsTask task = new GraphicsTask(construct.getTitle());
                    for (int i = taskIndex + construct.size() - 1; i >= taskIndex; i--) {
                        task.mergeWith((GraphicsTask) _tasks.elementAt(i));
                        _tasks.removeElementAt(i);
                    }
                    _tasks.insertElementAt(task, taskIndex);
                    reqIndex = construct.size() - 1;
                }
            }
        }
    }

    public boolean contains(SceneRequirement requirement) {
        for (int i = 0; i < _tasks.size(); i++)
            if (requirement.matches((GraphicsTask) _tasks.elementAt(i)))
                return true;
        return false;
    }

    public boolean isEmpty() {
        return _tasks.isEmpty();
    }

    public boolean isNull() {
        return false;
    }
}
